const service = require("../../service/activity.js");
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 1,
    listData: [],
    pageSize: 1000,
    pageNum: 1,
    noDatas: false,
    noData: false
  },
  getList(callback) {
    var self = this;
    let customerId = wx.getStorageSync("userInfo").id;
    service.getCouponList({
      //列表参数
      "data": {
        "cutomerId": customerId,
        "isPaging": 1,
      },
      "queryRequest": {
        "pageSize": self.data.pageSize,
        "pageNum": self.data.pageNum
      }

    },
      (res) => {
        console.log(res)
        callback(res);
      }
    );
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    // 订单列表
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(function (res) {
      let list = res.data.rows || []
      list.forEach(item => {
        item.couponSkuSkuList.forEach(skuItem => {
          skuItem.bgImage = skuItem.images.split(',')[0]
        })
      })
      if (res.data.rows.length == 0) {
        self.setData({
          noDatas: true,
          noData: false
        });
      } else if (res.data.rows.length > 0 && res.data.rows.length < 4) {
        self.setData({
          noData: true,
        });
      }
      self.setData({
        listData: list,
      });
      wx.hideLoading();
    });
  },
  receiveCouponTo(e) {
    let self = this;
    let couponId = e.currentTarget.dataset.index.couponId
    service.getReceiveCoupon({
      "data": {
        "couponId": couponId, //优惠券id
        "cutomerId": wx.getStorageSync('userInfo').id //客户id
      }
    }, res => {
      console.log(res)
      if (res.code == "00000") {
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 2000,
        })
        setTimeout(function () {
          self.onShow()
        }, 1000)
      } else if (res.code == "H1300") {
        Toast.fail(res.message);
      } else {
        Toast.fail(res.message);
      }
    })
  },
  getCouponDetailesTo(e) {
    let CouponId = e.currentTarget.dataset.index.couponId
    let type = e.currentTarget.dataset.index.type
    wx.navigateTo({
      url: '/pages/golddiscounts/couponDetail/index?CouponId=' + CouponId + '&type=' + type,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    // 请求数据
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    this.getList(function (res) {
      let list = res.data.rows || []
      list.forEach(item => {
        item.couponSkuSkuList.forEach(skuItem => {
          skuItem.bgImage = skuItem.images.split(',')[0]
        })
      })
      if (res.data.rows.length > 0 && res.data.rows.length < 4) {
        self.setData({
          listData: list,
          noData: true,
        });
      } else {
        self.setData({
          listData: list,
        });
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onPullDownRefresh: function () {
    return
    // 请求数据
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;
    this.getList(function (res) {
      let list = res.data.rows || []
      list.forEach(item => {
        item.couponSkuSkuList.forEach(skuItem => {
          skuItem.bgImage = skuItem.images.split(',')[0]
        })
      })
      if (res.data.rows.length > 0 && res.data.rows.length < 4) {
        self.setData({
          listData: list,
          noData: true,
        });
      } else {
        self.setData({
          listData: list,
        });
      }
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    return
    // 判读数据是否加载完毕
    if (this.data.noData) return;
    // 停止下拉刷新
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    const listData = this.data.listData;
    let pageNum = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++pageNum,
    });
    const self = this;
    self.getList(function (res) {

      if (res.data.rows.length == 0) {
        self.setData({
          noData: true,
        });
      } else {
        res.data.rows.forEach((item) => {
          listData.push(item);
        });
        let list = listData || []
        list.forEach(item => {
          item.couponSkuSkuList.forEach(skuItem => {
            skuItem.bgImage = skuItem.images.split(',')[0]
          })
        })
        self.setData({
          listData: listData,
        });
      }
    });
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})